<template>
  <div class="wrapper">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="search">
          <span>主播名:</span
          ><el-input
            v-model="checkcontent.name"
            placeholder="请输入主播姓名"
          ></el-input>
        </div>
        <div class="block">
          <span class="demonstration">选择日期：</span>
          <el-date-picker
            v-model="checkcontent.dateValue"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </div>

        <div class="buttongroup">
          <el-button type="primary" @click="check">查询</el-button>
          <el-button type="primary" @click="add">新增</el-button>
        </div>
      </div>
      <div>
        <el-button @click="backtabel">返回</el-button>
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <div>
              <el-form label-position="left" inline class="demo-table-expand">
                <div v-for="(item, index) in props.row.detail" :key="index">
                  <el-form-item label="商品名称">
                    <span>{{ item.shopname }}</span>
                  </el-form-item>
                  <el-form-item label="销售数量">
                    <span>{{ item.number }}</span>
                  </el-form-item>
                </div>
              </el-form>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="id" width="180"> </el-table-column>
        <el-table-column prop="name" label="名称" width="180">
        </el-table-column>

        <el-table-column prop="price" label="销售额" width="180">
        </el-table-column>
        <el-table-column label="日期">
          <template slot-scope="scope">
            <div>
              {{ scope.row.date | filterDate }}
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="edit(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="deleteUser(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage3"
          :page-size="page_size"
          layout="prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="主播id" prop="id">
            <el-input v-model="ruleForm.id" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="主播姓名" prop="name">
            <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="总销售额" prop="price">
            <el-input v-model="ruleForm.price" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="date">
            <el-date-picker
              v-model="ruleForm.date"
              align="right"
              type="date"
              placeholder="选择日期"
              :picker-options="pickerOptions2"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="具体明细" prop="detail" class="detail">
            <el-button class="addshop" @click="addshop" type="success"
              >增加商品</el-button
            >
            <div v-for="(item, index) in ruleForm.detail" :key="index">
              <span>商品名</span>
              <el-input v-model="item.shopname" autocomplete="off"></el-input>
              <span>商品数量</span>
              <el-input v-model="item.number" autocomplete="off"></el-input>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog title="提示" :visible.sync="dialogVisible2" width="30%">
        <el-form
          :model="ruleForm2"
          status-icon
          :rules="rules"
          ref="ruleForm2"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="主播id" prop="id">
            <el-input v-model="ruleForm2.id" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="主播姓名" prop="name">
            <el-input v-model="ruleForm2.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="总销售额" prop="price">
            <el-input v-model="ruleForm2.price" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="date">
            <el-date-picker
              v-model="ruleForm2.date"
              align="right"
              type="date"
              placeholder="选择日期"
              :picker-options="pickerOptions2"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="具体明细" prop="detail" class="detail">
            <el-button class="addshop" @click="addshop" type="success"
              >增加商品</el-button
            >
            <div v-for="(item, index) in ruleForm2.detail" :key="index">
              <span>商品名</span>
              <el-input v-model="item.shopname" autocomplete="off"></el-input>
              <span>商品数量</span>
              <el-input v-model="item.number" autocomplete="off"></el-input>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm2('ruleForm2')"
              >提交</el-button
            >
            <el-button @click="cancelForm2">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { log } from "util";
export default {
  components: {},
  props: {},
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      pickerOptions2: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          }
        ]
      },
      checkcontent: { dateValue: "", name: "" },
      totalData: [],
      tableData: [],
      page_size: 8,
      total: 0,
      currentPage3: 1,
      dialogVisible: false,
      dialogVisible2: false,
      ruleForm: {
        bill: "",
        id: "",
        name: "",
        price: "",
        date: "",
        detail: [
          {
            shopname: "",
            number: ""
          }
        ]
      },
      ruleForm2: {
        bill: "",
        id: "",
        name: "",
        price: "",
        date: "",
        detail: [
          {
            shopname: "",
            number: ""
          }
        ]
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        id: [{ required: true, message: "请输入id", trigger: "blur" }],
        date: [{ required: true, message: "请输入时间", trigger: "blur" }],
        price: [{ required: true, message: "请输入销售额", trigger: "blur" }],
        detail: [{ required: true, message: "请输入明细", trigger: "blur" }]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    check() {
      this.tableData = [];
      var name = this.checkcontent.name;
      var start = this.checkcontent.dateValue[0];
      var end = this.checkcontent.dateValue[1];

      if (name) {
        if (start && end) {
          this.totalData.forEach(item => {
            var date = new Date(item.date);
            if (item.name == name && start <= date && date <= end) {
              this.tableData.push(item);
            }
          });
        } else {
          this.totalData.forEach(item => {
            if (item.name == name) {
              this.tableData.push(item);
            }
          });
        }
      } else {
        if (start && end) {
          this.totalData.forEach(item => {
            var date = new Date(item.date);
            if (start <= date && date <= end) {
              this.tableData.push(item);
            }
          });
        } else {
          return this.$message({
            message: "请输入信息",
            type: "error"
          });
        }
      }
    },
    add() {
      this.ruleForm = {
        id: "",
        name: "",
        price: "",
        date: "",
        detail: [
          {
            shopname: "",
            number: ""
          }
        ]
      };
      this.dialogVisible = true;
    },
    addshop() {
      this.ruleForm.detail.push({
        shopname: "",
        number: ""
      });
    },
    edit(data) {
      this.ruleForm2 = data;
      this.dialogVisible2 = true;
    },
    deleteUser(data) {
      this.$confirm("您确定要删除该用户信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var billid = data.bill;
          this.totalData.forEach((item, index) => {
            if (item.bill == billid) {
              this.totalData.splice(index, 1);
            }
            this.tabelData();
          });
          this.tabelData();
          return this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          return this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    backtabel() {
      this.tableData = this.totalData.slice(0, this.page_size);
      this.checkcontent = { dateValue: "", name: "" };
    },
    handleCurrentChange(val) {
      console.log(val);
      this.tableData = this.totalData.slice(
        this.page_size * (val - 1),
        this.page_size * val
      );
    },
    tabelData() {
      this.tableData = this.totalData.slice(0, this.page_size);
      this.total = this.totalData.length;
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.ruleForm);
          this.totalData.unshift(this.ruleForm);
          this.dialogVisible = false;
          this.tabelData();
          return this.$message({
            message: "添加信息成功",
            type: "success"
          });
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    cancelForm() {
      this.ruleForm = {
        name: "",
        price: "",
        stock: "",
        band: "",
        type: "",
        remarks: ""
      };
      this.dialogVisible = false;
      this.$message("您已取消添加");
    },

    submitForm2(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //console.log(this.ruleForm2);
          var id = this.ruleForm2.bill;
          console.log(id);
          this.totalData.forEach(item => {
            if (item.bill === id) {
              item = this.ruleForm2;
            }
          });
          this.tabelData();
          this.dialogVisible2 = false;
          return this.$message({
            message: "修改成功",
            type: "success"
          });
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    cancelForm2() {
      this.ruleForm = {};
      this.dialogVisible2 = false;
      this.$message("您已取消编辑");
    },
    async getTotalData() {
      await this.$http.get("../../static/json/sales.json").then(res => {
        //console.log(res.data);
        this.totalData = res.data;
      });
      this.tabelData();
    }
  },
  created() {},
  mounted() {
    this.getTotalData();
  },
  filters: {
    filterDate(date) {
      var date = new Date(date);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      var str = year + "年" + month + "月" + day + "日";
      return str;
    }
  }
};
</script>
<style lang="scss" scoped>
.wrapper {
}
.search {
  font-size: 16px;
  span {
    display: inline-block;
    margin-right: 17px;
  }
  .el-input {
    width: 200px;
  }
}
.clearfix {
  display: flex;
  justify-content: space-between;
}
.detail {
  position: relative;
  .addshop {
    position: absolute;
    right: 0px;
    top: 6px;
    width: 100px;
    height: 30px;
    line-height: 6px;
    font-size: 14px;
    text-align: center;
  }
}
</style>
